package com.cls.yygh.cmn.controller;

import com.cls.yygh.cmn.service.DictService;
import com.cls.yygh.common.result.Result;
import com.cls.yygh.model.cmn.Dict;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @program: yygh
 * @description:
 * @author: LH
 * @create: 2021-11-01 10:36
 * @Platform: JDK 1.8.0_301
 **/
@RestController
@RequestMapping("/admin/cmn/dict")
@Api(tags = "数据字典接口")
@Slf4j
public class DictController {

    @Autowired
    private DictService dictService;

    //根据数据id查询子数据列表
    @PostMapping("/findChildData/{id}")
    @ApiOperation(value = "根据数据id查询子数据列表", notes = "根据数据id查询子数据列表", httpMethod = "POST",
            response = Result.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "字典id",
                    dataType = "Long", paramType = "path",required = true)
    })
    public Result findChildData(@PathVariable Long id){
        try{
            List<Dict> list = dictService.findChildData(id);
            return Result.ok(list);
        } catch (Exception e) {
            log.error("【/admin/cmn/dict/findChildData-根据数据id查询子数据列表接口异常 】", e);
            e.printStackTrace();
            return Result.fail();
        }
    }

    @GetMapping("/exportData")
    @ApiOperation(value = "导出数据字典", notes = "导出数据字典", httpMethod = "GET",
            response = Result.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public Result exportData(HttpServletResponse response){
        try {
            dictService.exportData(response);
            return Result.ok();
        } catch (Exception e) {
            log.error("【/admin/cmn/dict/exportData-导出数据字典接口异常 】", e);
            e.printStackTrace();
            return Result.fail();
        }
    }

    @PostMapping("/importData")
    @ApiOperation(value = "导入数据字典", notes = "导入数据字典", httpMethod = "POST",
            response = Result.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public Result importData(MultipartFile file){
        try {
            dictService.importData(file);
            return Result.ok();
        } catch (Exception e) {
            log.error("【/admin/cmn/dict/importData-导入数据字典接口异常 】", e);
            e.printStackTrace();
            return Result.fail();
        }
    }

    @ApiOperation(value = "获取数据字典名称")
    @PostMapping(value = "getName/{parentDictCode}/{value}")
    public String getName(
            @ApiParam(name = "parentDictCode",value = "上级编码",required = true)
            @PathVariable("parentDictCode") String parentDictCode,

            @ApiParam(name = "value",value = "值",required = true)
            @PathVariable("value") String value){

        return dictService.getNameByParentDictCodeAndValue(parentDictCode,value);

    }

    @ApiOperation(value = "获取数据字典名称")
    @PostMapping(value = "getName/{value}")
    public String getName(
            @ApiParam(name = "value", value = "值", required = true)
            @PathVariable("value") String value) {
        return dictService.getNameByParentDictCodeAndValue("", value);
    }

    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping(value = "/findByDictCode/{dictCode}")
    public Result<List<Dict>> findByDictCode(
            @ApiParam(name = "dictCode", value = "节点编码", required = true)
            @PathVariable String dictCode) {
        List<Dict> byDictCode = dictService.findByDictCode(dictCode);
        return Result.ok(byDictCode);
    }

}
